package com.streamer.app.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.streamer.app.R
import com.streamer.app.bean.CouponBean
import com.streamer.app.utils.ToastUtil

class CouponSelectionAdapter(
    private val coupons: MutableList<CouponBean>,
    private val onCouponSelected: (CouponBean) -> Unit
) : RecyclerView.Adapter<CouponSelectionAdapter.CouponVH>() {

    private var allCoupons = mutableListOf<CouponBean>()
    private var selectedCoupon: CouponBean? = null

    class CouponVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvCouponName: TextView = itemView.findViewById(R.id.tv_coupon_name)
        val container: View = itemView.findViewById(R.id.container)
        val tvCouponType: TextView = itemView.findViewById(R.id.tv_coupon_type)
        val tvCouponStock: TextView = itemView.findViewById(R.id.tv_coupon_stock)
        val viewSelected: View = itemView.findViewById(R.id.view_selected)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CouponVH {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_coupon_selection, parent, false)
        return CouponVH(view)
    }

    override fun onBindViewHolder(holder: CouponVH, position: Int) {
        val coupon = coupons[position]

        holder.tvCouponName.text = coupon.couponName
        if (coupon.type == "1") {
            holder.tvCouponType.text = "满减券"
        } else if (coupon.type == "2") {
            holder.tvCouponType.text = "折扣券"
        }

        if (coupon.isLimited == 1) {
            holder.tvCouponStock.text = "库存: ${coupon.total}"
        } else {
            holder.tvCouponStock.text = "不限量"

        }

        // 设置选中状态
        if (coupon.isSelected) {
            holder.container.setBackgroundResource(R.drawable.btn_gray_background)
        } else {
            holder.container.setBackgroundResource(R.drawable.white_square_bg)

        }

        holder.itemView.setOnClickListener {

            if (coupon.isExpiration != 1) {
                // 清除之前的选择
                coupons.forEach { it.isSelected = false }
                selectedCoupon = null

                // 设置当前选择
                coupon.isSelected = true
                selectedCoupon = coupon

                // 通知回调
                onCouponSelected(coupon)

                // 刷新UI
                notifyDataSetChanged()
            } else {
                ToastUtil.toastShortMessage("优惠卷已过期")
            }

        }
    }

    override fun getItemCount(): Int = coupons.size

    fun updateData(newCoupons: List<CouponBean>) {
        allCoupons.clear()
        allCoupons.addAll(newCoupons)

        coupons.clear()
        coupons.addAll(newCoupons)

        notifyDataSetChanged()
        Log.d("CouponSelectionAdapter", "更新优惠券数据: ${coupons.size} 个")
    }

    fun filterCoupons(query: String) {
        if (query.isEmpty()) {
            coupons.clear()
            coupons.addAll(allCoupons)
        } else {
            val filtered = allCoupons.filter {
                it.name.contains(query, ignoreCase = true) ||
                        it.type.contains(query, ignoreCase = true)
            }
            coupons.clear()
            coupons.addAll(filtered)
        }
        notifyDataSetChanged()
        Log.d("CouponSelectionAdapter", "过滤优惠券: 查询='$query', 结果=${coupons.size} 个")
    }

    fun getSelectedCoupon(): CouponBean? = selectedCoupon
} 